<?php
/**
 * Create by wenyi
 * E-mail: wesley_prc@163.com
 * Description: vue-admin-element-api
 * Date: 2024/2/21 17:17
 * Update: 2024/2/21 17:17
 */

namespace app\api\controller;

use app\BaseController;
use think\facade\Db;

class SystemBBB extends BaseController
{
    public function index()
    {
        $data = ['version'=>'v1.3.9','server'=>'DSIS'];
        return returnResult(config("config.code.success"),'测试',$data);
    }
    public function me()
    {
        $data = [
            'id'=>'5',
            'jobNo'=>'001',
            'avatar'=>null,
            'name'=>"管*员",
            'gender'=>0,
            'account'=>'001',
            'hisCode'=>'001',
            'type'=>0,
            'pinyin'=>'GUANLIYUAN',
            'department'=>[
                'id'=>11,
                'departmentNo'=>'8901',
                'departmentName'=>'日间手术中心',
            ],
            'jobPosition'=>null,
            'hospitalArea'=>null,
            'role'=>[
               [ 'id'=>12,
                   'slug'=>'administrator',
                   'name'=>'管理员'
               ],
                [ 'id'=>13,
                    'slug'=>'central_doctor',
                    'name'=>'中心医生'
                ],
                [ 'id'=>14,
                    'slug'=>'central_nurse',
                    'name'=>'中心护士'
                ],
                'status'=>true
            ]
        ];
        return returnResult(config("config.code.success"),'测试',$data);
    }
    public function menus()
    {
        $q = Db::table('menu')->select();
//        $menuApiList = $this->getMenuApiIds($q);
        $ss = $this->sort($q);
    var_dump($ss);
        return returnResult(config("config.code.success"),'测试',array_merge($list));
    }
    protected function sort($data,$pid=0,$level=0){
        //此处数据必须是静态数组，不然递归的时候每次都会声明一个新的数组
        static $arr = array();
        foreach ($data as $key=>$value){
            if($value['parentId'] == $pid){
                $value["level"]=$level;
                $arr[]=$value;
                //unset()用于销毁指定的变量
                unset($this->data[$key]);
                $this->sort($data,$value['id'],$level+1);
            }
        }
        return $arr;
    }
    //整理菜单的api ID集
    public function getMenuApiIds($menuList){
        try {
            foreach ($menuList as &$item) {
                if (!empty($item['menuApi'])) {
                    $item['apiIds'] = helper::getArrayColumn($item['menuApi'], 'id');
                }
            }
            return $menuList;
        }catch (\Exception $e){
            exception('MenuBus.getMenuApiIds',$e);
        }
    }
    /**
     * 获取树状菜单列表
     * @param $menuList
     * @param int $parentId
     * @return array
     */
    protected function getTreeData($menuList, int $parentId = 0)
    {
        try {
            $data = [];
            foreach ($menuList as $key => $item) {
                if ($item['parent_id'] == $parentId) {
                    $children = $this->getTreeData($menuList, (int)$item['id']);
                    !empty($children) && $item['children'] = $children;
                    $data[] = $item;
                    unset($menuList[$key]);
                }
            }
            return $data;
        }catch (\Exception $e){
            exception('MenuBus.getTreeData',$e);
        }
    }
}